package com.open.capacity.user.controller;


import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.user.enums.AuthorizeItemTypes;
import com.open.capacity.user.model.SysAuthorize;
import com.open.capacity.user.service.SysAuthorizeService;
import com.open.capacity.user.vo.SysAuthorizeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者 ftquan E-mail: 464725602@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 功能授权
 */
@RestController
@Api(tags = "功能授权模块api")
public class SysAuthorizeController {

    private static Logger logger = LoggerFactory.getLogger(SysAuthorizeController.class);


    @Autowired
    private SysAuthorizeService authorizeService;


    /**
     * 查询功能授权
     *
     * @param objectId
     * @return
     */
    @PreAuthorize("hasAuthority('authorize:get/authorizes')")
    @ApiOperation(value = "查询功能授权")
    @GetMapping("/authorizes/{objectId}/authorizes")
    public Result<SysAuthorizeVo> findAuthorizes(@PathVariable(value = "objectId") String objectId) {
        SysAuthorizeVo vo = new SysAuthorizeVo();
        List<SysAuthorize> list = authorizeService.findByObjectId(objectId);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(r -> {
                if (r.getItemType().equals(AuthorizeItemTypes.MENU.getCode())) {
                    if (vo.getMenus() == null) {
                        vo.setMenus(new ArrayList<>());
                    }
                    vo.getMenus().add(r.getItemId());
                } else if (r.getItemType().equals(AuthorizeItemTypes.BUTTON.getCode())) {
                    if (vo.getBtns() == null) {
                        vo.setBtns(new ArrayList<>());
                    }
                    vo.getBtns().add(r.getItemId());
                } else if (r.getItemType().equals(AuthorizeItemTypes.COLUMN.getCode())) {
                    if (vo.getCols() == null) {
                        vo.setCols(new ArrayList<>());
                    }
                    vo.getCols().add(r.getItemId());
                } else if (r.getItemType().equals(AuthorizeItemTypes.FORM.getCode())) {
                    if (vo.getForms() == null) {
                        vo.setForms(new ArrayList<>());
                    }
                    vo.getForms().add(r.getItemId());
                }
            });
        }

        return Result.succeed(vo);
    }


    /**
     * 功能授权新增或者更新
     *
     * @param sysAuthorize
     * @return
     */
    @PreAuthorize("hasAnyAuthority('authorize:post/authorizes','authorize:put/authorizes')")
    @ApiOperation(value = "保存功能授权")
    @PostMapping("/authorizes/saveOrUpdate")
    @LogAnnotation(module = "权限", name = "更新")
    public Result saveOrUpdate(@RequestBody SysAuthorizeVo sysAuthorize) {
        return authorizeService.saveOrUpdate(sysAuthorize);
    }

}
